/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.pi.departments;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import researchgrants.parts.db.Db;

/**
 *
 * @author DOStudent1
 */
public class Department {
    public static Department getByDepartmentName(String departmentName) {
        return(new Department(departmentName));
    }

    private String departmentName;
    private School school;
    
    private Department(String departmentName) {
        this.departmentName=departmentName;
        syncDataFromDb();
    }

    private void syncDataFromDb() {
        try {
            Connection conn = Db.openDbConnection();
            PreparedStatement prepareStatement = Db.createPreparedStatement(conn, "SELECT SchoolName FROM tblDepartments WHERE DepartmentName=?");
            prepareStatement.setString(1, departmentName);
            ResultSet departmentResultSet = prepareStatement.executeQuery();
            Db.moveToFirstLine(departmentResultSet);

            String schoolName=(String) Db.getColumn(departmentResultSet, 1);
            school=School.getBySchoolName(schoolName);
            departmentResultSet.close();
            prepareStatement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(Department.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Department.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public String getDepartmentName() {
        return departmentName;
    }
    
    public School getSchool() {
        return(school);
    }
 

    public static List<Department> getDepartmentsOfSchool(String school) {
        List<Department> departmentslist = null;
        try {
            departmentslist = new LinkedList<Department>();
            Connection conn = Db.openDbConnection();
            PreparedStatement prepareStatement = Db.createPreparedStatement(conn, "SELECT DepartmentName FROM tblDepartments WHERE SchoolName=? ORDER BY DepartmentName");
            prepareStatement.setString(1, school);
            ResultSet departmentResultSet = prepareStatement.executeQuery();

            while (departmentResultSet.next()) {
                departmentslist.add(Department.getByDepartmentName((String) Db.getColumn(departmentResultSet, 1)));
            }

            departmentResultSet.close();
            prepareStatement.close();
            Db.closeDbConnection(conn);
        } catch (SQLException ex) {
            Logger.getLogger(Department.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(Department.class.getName()).log(Level.SEVERE, null, ex);
        }
        return departmentslist;
    }
    
    public String toListId() {
        return(departmentName);
    }

    public String toListValue() {
        return(departmentName);
    }
}
